package com.cdc.dp;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;

public class ScreenAdaptation {
    private ScreenAdaptation() {
    }


    public static String getScreenAndModel(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return getScreen(metrics,activity);
    }


    public static float dpToPx(Activity activity,float dp){
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float px=dp*metrics.density+0.5f;
       return px;
    }

    public static float pxToDp(Activity activity,float px){
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float dp=px/(metrics.density)+0.5f;
        return dp;
    }

    private static String getScreen(DisplayMetrics metrics,Activity activity){
        String string="";
        float fl=metrics.density;
        int dpi=metrics.densityDpi;
        int heightPixels=metrics.heightPixels;
        int widthPixels=metrics.widthPixels;
        float scaledDensity=metrics.scaledDensity;
        float xdpi=metrics.xdpi;
        float ydpi=metrics.ydpi;
        float xdp=widthPixels/fl;
        float ydp=heightPixels/fl;
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("density",String.valueOf(fl));
            jsonObject.put("dpi",String.valueOf(dpi));
            jsonObject.put("heightPixels",String.valueOf(heightPixels));
            jsonObject.put("widthPixels",String.valueOf(widthPixels));
            jsonObject.put("scaledDensity",String.valueOf(scaledDensity));
            jsonObject.put("xdpi",String.valueOf(xdpi));
            jsonObject.put("ydpi",String.valueOf(ydpi));
            jsonObject.put("xdp",String.valueOf(xdp));
            jsonObject.put("ydp",String.valueOf(ydp));
            jsonObject.put("os",getSystemVersion());
            jsonObject.put("brand",getDeviceBrand());
            jsonObject.put("model",getSystemModel());
            try {
                PackageManager manager = activity.getPackageManager();
                PackageInfo info = manager.getPackageInfo(activity.getPackageName(), 0);
                String version = info.versionName;
                int versionCode=info.versionCode;
                jsonObject.put("versionName",version);
                jsonObject.put("versionCode",String.valueOf(versionCode));
                String baseDpi=activity.getResources().getString(R.string.base_dpi);
                jsonObject.put("baseDpi",baseDpi);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            string=jsonObject.toString();
            Log.e("screen",string);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return string;
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }
}
